package com.bot.adms.controller.apply;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.bo.ApplyContentBo;
import com.bot.adms.model.bo.ApplyInfoBo;
import com.bot.adms.model.vo.ApplyInfoVo;
import com.bot.adms.service.impl.ApplyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询投放申请控制层
 * @author: 刘威林
 */
@WebServlet("/apply/search")
public class GetApplyListServlet extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //响应体创建
        JSONObject jsonObject = new JSONObject();
        //success定义【true成功，false失败】
        boolean success = false;
        //响应码定义【响应码,200-成功,500-失败】
        int code = 500;
        String msg = "查询失败";
        //data定义【用来返回查看的具体数据】
        JSONArray data = new JSONArray();

        //1.从请求参数中获取搜索关键字
        String status = req.getParameter("status");
        String title = req.getParameter("title");
        //2.创建ApplyInfoBo对象用于封装查询条件
        ApplyInfoBo applyInfoBo = new ApplyInfoBo();
        ApplyContentBo applyContentBo = new ApplyContentBo();
        //3.如果搜索关键字不为空，则设置项目名称作为查询条件
        if (status!=null && !status.trim().isEmpty()){
            applyInfoBo.setStatus(Integer.parseInt(status));
        }
        if (title!=null && !title.trim().isEmpty()){
            applyContentBo.setTitle(title);
        }
        applyInfoBo.setApplyContentBo(applyContentBo);
        //4.调用业务逻辑
        ApplyServiceImpl applyService = new ApplyServiceImpl();
        //5.调用服务层方法查询投放申请列表
        List<ApplyInfoVo> applyInfoBoList = applyService.getApplyList(applyInfoBo);

        //6.响应结果
        if (applyInfoBoList!= null && !applyInfoBoList.isEmpty()){
            success = true;
            code = 200;
            msg = "查询成功";
            data = (JSONArray) JSON.toJSON(applyInfoBoList);
        }else {
            success = false;
            code = 500;
            msg = "未找到该条件对应数据";
        }
        //组装JSON
        jsonObject.put("success", success);
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        //7.转换成JSON字符
        String json = JSON.toJSONString(jsonObject);


        //响应字符编码为UTF-8
        resp.setCharacterEncoding("utf-8");
        //设置响应内容类型为JSON格式
        resp.setContentType("application/json;charset=utf-8");
        //8.获取输出流并将JSON数据写入响应
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(json.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}
